package com.yue.lin.service;

import com.yue.lin.common.PassEncry;
import com.yue.lin.domain.Permission;
import com.yue.lin.domain.Role;
import com.yue.lin.domain.User;
import com.yue.lin.repository.UserRepository;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhengyl
 * @description
 * @date 2020-04-02 10:11
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PermissionService permissionService;

    public boolean add(User user) {
        user.setPassword(PassEncry.encryption(user.getPassword(), user.getSalt()));
        Role role1 = new Role();
        role1.setName("a");
        Role role2 = new Role();
        role2.setName("b");
        List<Role> roles = new ArrayList<>();
        roles.add(role1);
        roles.add(role2);
        user.setRoles(roles);
        return userRepository.save(user) != null;
    }

    public User current() {
        Subject subject = SecurityUtils.getSubject();
        return (User) subject.getPrincipal();
    }

    /**
     * 获取当前用户所有的权限树
     *
     * @return
     */
    public List<Permission> allPermissionWithRole() {
        User user = current();
        //查询当前用户的所有权限
        List<Permission> list = permissionService.allPermissionByUid(user.getId());
        //权限树结构显示 父权限和子权限一起保存的情况 由父权限递归子权限
        return parsePermissionTree(list);
    }

    public List<Permission> allPermissionWithRole2() {
        User user = current();
        //查询当前用户的所有权限
        List<Permission> list = permissionService.allPermissionByUid(user.getId());
        //所有权限
        List<Permission> all = permissionService.findAll();
        //权限树结构显示 只保存叶节点的情况 由子权限递归父权限
        return parsePermissionTree2(list, all, new ArrayList<Permission>());
    }

    /**
     * @param list 数据库里面获取到的全量菜单列表
     * @return
     * @方法名: parseMenuTree<br>
     * @描述: 组装菜单<br>
     */
    public List<Permission> parsePermissionTree(List<Permission> list) {
        List<Permission> result = new ArrayList<Permission>();
        // 1、获取第一级节点
        for (Permission permission : list) {
            if (StringUtils.isBlank(permission.getPid())) {
                result.add(permission);
            }
        }
        // 2、递归获取子节点
        for (Permission parent : result) {
            recursiveTree(parent, list);
        }
        return result;
    }

    public Permission recursiveTree(Permission parent, List<Permission> list) {
        for (Permission permission : list) {
            if (parent.getId().equals(permission.getPid())) {
                permission = recursiveTree(permission, list);
                parent.getChildren().add(permission);
            }
        }
        return parent;
    }

    private List<Permission> parsePermissionTree2(List<Permission> permissionList, List<Permission> all, List<Permission> list) {
        List<Permission> collect = all.stream()
                .filter(permission -> permissionList.stream().map(Permission::getPid).collect(Collectors.toList()).contains(permission.getId()))
                .collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            for (Permission permission : collect) {
                for (Permission permission1 : permissionList) {
                    if (StringUtils.isNotBlank(permission1.getPid()) && permission1.getPid().equals(permission.getId())) {
                        permission.getChildren().add(permission1);
                    }
                }
                if (StringUtils.isBlank(permission.getPid())) {
                    list.add(permission);
                }
            }
            parsePermissionTree2(collect, all, list);
        }
        return list;
    }
}
